home *** CD-ROM | disk | FTP | other *** search
/ Revista do CD-ROM 118 / cd-rom 118.iso / aplic / open / openofficeorg1.cab / FormWizard.xba < prev    next >
Encoding:
Extensible Markup Language  |  2004-08-03  |  13.6 KB  |  427 lines

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE script:module PUBLIC "-//OpenOffice.org//DTD OfficeDocument 1.0//EN" "module.dtd">
  3. <script:module xmlns:script="http://openoffice.org/2000/script" script:name="FormWizard" script:language="StarBasic">Option Explicit
  4.  
  5. Public DocumentName as String
  6. Public FormPath as String
  7. Public WizardPath as String
  8. Public WebWizardPath as String
  9. Public WorkPath as String
  10. Public TempPath as String
  11. Public TexturePath as String
  12. Public sQueryName as String
  13. Public oDBConnection as Object
  14. Public bWithBackGraphic as Boolean
  15. Public bNeedFieldRefresh as Boolean
  16. Public oDBForm as Object
  17. Public oColumns() as Object
  18. Public sDatabaseList() as String
  19. Public TableNames() as String
  20. Public QueryNames() as String
  21. Public FieldNames() as String
  22. Public ImgFieldNames() as String
  23. Public oDBContext as Object
  24. Public oUcb as Object
  25. Public oDocInfo as Object
  26. Public WidthList(15,3)
  27. Public ImgWidthList(3,3)
  28. Public sDBName as String
  29. Public Tablename as String
  30. Public Const SBSIZETEXT = "The quick brown fox jumps over the lazy dog. The quick brown fox jumps over the lazy dog."
  31. Public bDisposeDoc as Boolean
  32. Public bDebug as Boolean
  33. 'Public bStartUp as Boolean
  34. Public bConnectionIsovergiven as Boolean
  35. Public FormName As String
  36. Public sFormUrl as String
  37. Public oFormDocuments
  38.  
  39.  
  40. ' The macro can be called in 4 possible scenarios:
  41. ' Scenario 1. No parameters at given
  42. ' Scenario 2: Only Datasourcename is given, but no connection and no Content
  43. ' Scenario 3: a data source and a connection are given
  44. ' Scenario 4: all parameters (data source name, connection, object type and object) are given
  45.  
  46. Sub Main()
  47. Dim oLocDBContext as Object
  48. Dim oLocConnection as Object
  49.  
  50. ' Scenario 1. No parameters at given
  51.     MainWithDefault()
  52.  
  53. ' Scenario 2: Only Datasourcename is given, but no connection and no Content
  54. '    MainWithDefault("Bibliography")
  55.  
  56. ' Scenario 3: a data source and a connection are given
  57. '    oLocDBContext = CreateUnoService("com.sun.star.sdb.DatabaseContext")
  58. '    oLocConnection = oLocDBContext.GetByName("Bibliography").GetConnection("","")
  59. '    MainWithDefault("Bibliography", oLocConnection)
  60.  
  61. ' Scenario 4: all parameters (data source name, connection, object type and object) are given
  62. '    oLocDBContext = CreateUnoService("com.sun.star.sdb.DatabaseContext")
  63. '    oLocConnection = oLocDBContext.GetByName("Bibliography").GetConnection("","")
  64. '    MainWithDefault("Bibliography", oLocConnection, com.sun.star.sdb.CommandType.TABLE, "biblio")
  65. End Sub
  66.  
  67.  
  68. Sub MainWithDefault(Optional DatasourceName as String, Optional oConnection as Object, Optional CommandType as Integer, Optional sContent as String)
  69. Dim i as Integer
  70. Dim SelCount as Integer
  71. Dim RetValue as Integer
  72. Dim SelList(0) as Integer
  73. Dim LocList() as String
  74.     SelList(0) = 0
  75.     BasicLibraries.LoadLibrary("Tools")
  76.     BasicLibraries.LoadLibrary("WebWizard")
  77.     bDebug = False
  78.     If Not bDebug Then
  79.         On Local Error GoTo WIZARDERROR
  80.     End If
  81.     OpenFormDocument()
  82.     CurArrangement = 0
  83.     bControlsareCreated = False
  84.     bEnableBinaryOptionGroup = False
  85.     bDisposeDoc = True
  86.     MaxIndex = -1
  87.     If Not InitResources("Formwizard","dbw") Then
  88.         Exit Sub
  89.     End If
  90.     oDBContext = CreateUnoService("com.sun.star.sdb.DatabaseContext")
  91.     oUcb = createUnoService("com.sun.star.ucb.SimpleFileAccess")
  92.     If GetFormWizardPaths() = False Then
  93.         Exit Sub
  94.     End If
  95.     oDocument.GetCurrentController().Frame.ComponentWindow.Enable = False
  96.     oProgressBar.Value = 10
  97.     LoadLanguage()
  98.     oProgressBar.Value = 20
  99.     InitializeWidthList()
  100.     oProgressBar.Value = 30
  101.     Styles() = getListBoxArrays(oUcb, "/stl")
  102.     CurIndex = GetCurIndex(DialogModel, Styles(), 2)
  103.     oProgressBar.Value = 40
  104.     ConfigurePageStyle()
  105.     oProgressBar.Value = 50
  106.     InitializeLabelValues()
  107.     bNeedFieldRefresh = True
  108.     SetDialogLanguage()
  109. '    bStartUp = true
  110.     With DialogModel
  111.         .cmdBack.Enabled = False
  112.         .cmdGoOn.Enabled = False
  113.         .lblTables.Enabled = False
  114.         .lstSelFields.Tag = False
  115.         .Step = 1
  116.     End With
  117.     oProgressBar.Value = 60
  118.     bConnectionIsovergiven = Not IsMissing(oConnection)
  119.     If Not IsMissing(DataSourceName) Then
  120.         sDBName = DataSourceName
  121.         If Not IsMissing(oConnection) Then
  122.             ' Scenario 3: a data source and a connection are given
  123.             Set oDBConnection = oConnection
  124.             oDataSource = oDBContext.GetByName(DataSourceName)
  125.             DialogModel.lstTables.Enabled = True
  126.             DialogModel.lblTables.Enabled = True
  127.             If GetDBMetaData() Then
  128.                 LocList() = AddListToList(TableNames(), QueryNames())
  129.                 iCommandTypes = CreateCommandTypeList()
  130.                 If Not IsMissing(sContent) Then
  131.                     ' Scenario 4: all parameters (data source name, connection, object type and object) are given
  132.                     DialogModel.lstTables.StringItemList() = LocList()
  133.                     iCommandTypes() = CreateCommandTypeList()
  134.                     SelCount = CountItemsInArray(DialogModel.lstTables.StringItemList(), sContent)
  135.                     If SelCount = 1 Then
  136.                         DlgFormDB.GetControl("lstTables").SelectItem(sContent, True)
  137.                     Else
  138.                         If CommandType = com.sun.star.sdb.CommandType.QUERY Then
  139.                             SelIndex = IndexInArray(sContent, QueryNames()
  140.                             DlgFormDB.GetControl("lstTables").SelectItemPos(SelIndex, True)
  141.                         ElseIf CommandType = com.sun.star.sdb.CommandType.TABLE Then
  142.                             SelIndex = IndexInArray(sContent, TableNames()
  143.                             DlgFormDB.GetControl("lstTables").SelectItemPos(Ubound(QueryNames()+1 + SelIndex, True)
  144.                         End If
  145.                     End If
  146.                     CurCommandType = CommandType
  147.                     FillUpFieldsListbox(False)
  148.                 Else
  149.                     LocList() = AddListToList(Array(sSelectDBTable), LocList())
  150.                     DialogModel.lstTables.StringItemList() = LocList()
  151. '                    bSelectContent = True
  152.                     DialogModel.lstTables.SelectedItems() = Array(0)
  153.  
  154.                 End If
  155.             End If
  156.         Else
  157.             ' Scenario 2: Only Datasourcename is given, but no connection and no Content
  158.             GetSelectedDBMetaData(sDBName)
  159.         End If
  160.     Else
  161.         ' Scenario 1: No parameters are given
  162.         ToggleListboxControls(DialogModel, False)
  163.     End If
  164.     oProgressBar.Value = 80
  165.     bWithBackGraphic = LoadNewStyles(oDocument, DialogModel, CurIndex, Styles(CurIndex, 8), Styles(), TexturePath)
  166.     DlgFormDB.Title = WizardTitle(1)
  167.     DialogModel.lstStyles.StringItemList() = ArrayfromMultiArray(Styles, 1)
  168.     DialogModel.lstStyles.SelectedItems() = SelList()
  169.     ControlCaptionsToStandardLayout()
  170.     oDocument.GetCurrentController().Frame.ComponentWindow.Enable = True
  171.     oProgressBar.Value = 90
  172.     DialogModel.imgTheme.ImageURL = FormPath & "FormWizard_1.bmp"
  173.     DialogModel.imgTheme.BackGroundColor = RGB(0,60,126)
  174.     ToggleDatabasePage(True)
  175.     oProgressBar.Value = 100
  176.     DlgFormDB.GetControl("lstTables").SetFocus()
  177.     oProgressbar.End
  178.     RetValue = DlgFormDB.Execute()
  179.     DlgFormDB.Dispose()
  180.     If bDisposeDoc Then
  181.         Dim aPropertyValues(2) as new com.sun.star.beans.PropertyValue
  182.         oFormDocuments = oDataSource.getFormDocuments()
  183.         DlgFormDB.Dispose()
  184.         oDocument.dispose()
  185.         Dim    bLinkExists as Boolean
  186.         i = 1
  187.         Dim FormBaseName  as String
  188.         FormBaseName = FormName
  189.         Do                                 
  190.             bLinkExists = oFormDocuments.HasbyHierarchicalName(FormName)
  191.             If bLinkExists Then
  192.                 i = i + 1
  193.                 FormName = FormBaseName & "_" & i
  194.             End If
  195.         Loop Until Not bLinkExists
  196.         aPropertyValues(0).Name = "Name"
  197.         aPropertyValues(0).Value = FormName
  198.         aPropertyValues(1).Name = "Parent"
  199.         aPropertyValues(1).Value = oFormDocuments()
  200.         aPropertyValues(2).Name = "URL"
  201.         aPropertyValues(2).Value = sFormUrl
  202.         Dim oDBDocument
  203.         oDBDocument = oFormDocuments.createInstanceWithArguments("com.sun.star.sdb.DocumentDefinition", aPropertyValues())
  204.         oFormDocuments.insertbyName(FormName, oDBDocument)
  205.     ElseIf RetValue = 0 Then
  206.         RemoveNirwanaShapes()
  207.     End If
  208.     If ((Not IsNull(oDBConnection)) And (Not bConnectionIsovergiven)) Then
  209.         oDBConnection.Dispose()
  210.     End If    
  211. WIZARDERROR:
  212.     If Err <> 0 Then
  213.         Msgbox(sMsgErrMsg, 16, GetProductName())
  214.         Resume LOCERROR
  215.         LOCERROR:
  216.     End If
  217. End Sub
  218.  
  219.  
  220. Sub FormGetFields()
  221. Dim i as Integer
  222. '    If bSelectContent Then
  223. '        bSelectContent = False
  224. '        Exit Sub
  225. '    End If
  226.     DeleteFirstListBoxEntry("lstTables", sSelectDBTable)
  227.     ToggleDatabasePage(False)
  228.     FillUpFieldsListbox(True)
  229.     ToggleDatabasePage(True)
  230. End Sub
  231.  
  232.  
  233. Sub FillUpFieldsListbox(bGetCommandType as Boolean)
  234. Dim SelIndex as Integer
  235. Dim QueryIndex as Integer
  236.     If Not bDebug Then
  237.         On Local Error GoTo NOFIELDS
  238.     End If
  239.     SelIndex = DlgFormDB.GetControl("lstTables").getSelectedItemPos() '.SelectedItems())
  240.     If SelIndex > -1 Then
  241.         If bGetCommandType Then
  242.             CurCommandType = iCommandTypes(SelIndex)
  243.         End If
  244.         If CurCommandType = com.sun.star.sdb.CommandType.QUERY Then
  245.             QueryIndex = SelIndex  - Ubound(Tablenames()) - 1
  246.             Tablename = QueryNames(QueryIndex)
  247.             oColumns = oDBConnection.Queries.GetByName(TableName).Columns
  248.         Else
  249.             Tablename = Tablenames(SelIndex)
  250.             oColumns = oDBConnection.Tables.GetByName(Tablename).Columns
  251.         End If
  252.         If GetSpecificFieldNames() <> -1 Then
  253.             ToggleListboxControls(DialogModel, True)
  254.             Exit Sub
  255.         End If
  256.     End If
  257.     EmptyFieldsListboxes()
  258. NOFIELDS:
  259.     If Err <> 0 Then
  260.         MsgBox sMsgErrCouldNotOpenObject, 16, sMsgWizardName
  261.     End If
  262. End Sub
  263.  
  264.  
  265. Sub PreviousStep()
  266.     If Not bDebug Then
  267.         On Local Error GoTo WIZARDERROR
  268.     End If
  269.     With DialogModel
  270.         .Step = 1
  271.         .cmdBack.Enabled = False
  272.         .cmdGoOn.Enabled = True
  273.         .lstSelFields.Tag = Not bControlsareCreated
  274.         .cmdGoOn.Label = sGoOn
  275.         .imgTheme.ImageUrl = FormPath & "FormWizard_1.bmp"
  276.     End With
  277.     FormSetMoveRights()
  278. WIZARDERROR:
  279.     If Err <> 0 Then
  280.         Msgbox(sMsgErrMsg, 16, GetProductName())
  281.         Resume LOCERROR
  282.         LOCERROR:
  283.     End If
  284. End Sub
  285.  
  286.  
  287. Sub NextStep()
  288.     If Not bDebug Then
  289.         On Local Error GoTo WIZARDERROR
  290.     End If
  291.     Select Case DialogModel.Step
  292.         Case 1
  293.             bControlsAreCreated = Not (cBool(DialogModel.lstSelFields.Tag))
  294.             If Not bControlsAreCreated Then
  295.                 GetTableMetaData()
  296.                 CreateDBForm()
  297.                 RemoveShapes()
  298.                 InitializeLayoutSettings()
  299.                 oDBForm.Load
  300.             End If
  301.             DialogModel.cmdGoOn.Label = sReady
  302.             DialogModel.cmdBack.Enabled = True
  303.             DialogModel.Step = 2
  304.             bDisposeDoc = False
  305.         Case 2
  306.             StoreForm()
  307.             DlgFormDB.EndExecute()
  308.             exit Sub
  309.     End Select
  310.     DialogModel.imgTheme.ImageUrl = FormPath & "FormWizard_" & DialogModel.Step & ".bmp"
  311.     DlgFormDB.Title = WizardTitle(DialogModel.Step)
  312. WIZARDERROR:
  313.     If Err <> 0 Then
  314.         Msgbox(sMsgErrMsg, 16, GetProductName())
  315.         Resume LOCERROR
  316.         LOCERROR:
  317.     End If
  318. End Sub
  319.  
  320.  
  321. Sub InitializeLayoutSettings()
  322.     SwitchArrangementButtons(cTabled)
  323.     SwitchAlignMode(SBALIGNLEFT)
  324.     SwitchBorderMode(SB3DBORDER)
  325.     ToggleBorderGroup(bControlsAreCreated)
  326.     ToggleAlignGroup(bControlsAreCreated)
  327.     ArrangeControls()
  328.     If OldAlignMode <> 0 Then
  329.         DlgFormDB.GetControl("optAlign2").Model.State = 0
  330.     End If
  331. End Sub
  332.  
  333.  
  334. Sub ToggleDatabasePage(bDoEnable as Boolean)
  335.     With DialogModel
  336.         .cmdBack.Enabled = False
  337.         .cmdHelp.Enabled = bDoEnable
  338.         .cmdGoOn.Enabled = Ubound(DialogModel.lstSelFields.StringItemList()) <> -1
  339.         .hlnBinaries.Enabled = ((bDoEnable = True) And (bEnableBinaryOptionGroup = True))
  340.         .optIgnoreBinaries.Enabled = ((bDoEnable = True) And (bEnableBinaryOptionGroup = True))
  341.         .optBinariesasGraphics.Enabled = ((bDoEnable = True) And (bEnableBinaryOptionGroup = True))
  342.     End With
  343. End Sub
  344.  
  345.  
  346. ' This Sub is called from the Procedure "StoreDocument" in the "Tools" Library
  347. Sub CommitLastDocumentChanges(sTargetPath as String)
  348. Dim i as Integer
  349. Dim sBookmarkName as String
  350. Dim oDBBookmarks as Object
  351. Dim bLinkExists as Boolean
  352. Dim sBaseBookmarkName as String
  353.     sBookmarkName = GetFileNamewithoutExtension(FileNameoutofPath(sTargetPath))
  354.     sBaseBookmarkName = sBookmarkName
  355.     oDBBookmarks = oDataSource.GetBookmarks()
  356.     i = 1
  357.     Do
  358.         bLinkExists = oDBBookmarks.HasbyName(sBookmarkName)
  359.         If bLinkExists Then
  360.             i = i + 1
  361.             sBookmarkName = sBaseBookmarkName & "_" & i
  362.         Else
  363.             oDBBookmarks.insertByName(sBookmarkName, sTargetPath)
  364.         End If
  365.     Loop Until Not bLinkExists
  366.     bDisposeDoc = False
  367.     GroupShapesTogether()
  368.     ToggleDesignMode(oDocument)
  369.     oDBForm.Reload()
  370. End Sub
  371.  
  372.  
  373. Sub StoreFormInDatabase()
  374.     Dim NoArgs() as new com.sun.star.beans.PropertyValue    
  375.     FormName = "Form_" & sDBName & "_" & TableName & ".sxw"
  376.     sFormUrl = TempPath & "/" & FormName
  377.     oDocument.StoreAsUrl(sFormUrl, NoArgs())
  378.     bdisposeDoc = true
  379.     DlgFormDB.Endexecute()
  380. End Sub
  381.  
  382.  
  383.  
  384. Sub StoreForm()
  385. Dim sTargetPath as String
  386. Dim TypeNames(0,2) as String
  387. Dim oMasterKey as Object
  388. Dim oTypes() as Object
  389.     oMasterKey = GetRegistryKeyContent("org.openoffice.TypeDetection.Types/")
  390.     oTypes() = oMasterKey.Types
  391.     TypeNames(0,0) = GetFilterName("StarOffice XML (Writer)")
  392.     TypeNames(0,1) = "*.sxw"
  393.     TypeNames(0,2) = ""
  394.     StoreFormInDatabase()
  395. '    sTargetPath = StoreDocument(oDocument, TypeNames(), "Form_" & sDBName & "_" & TableName & ".sxw", WorkPath, 1)
  396. End Sub
  397.  
  398.  
  399.  
  400. Sub EmptyFieldsListboxes()
  401. Dim NullList() as String
  402.     ToggleListboxControls(DialogModel, False)
  403.     DialogModel.lstFields.StringItemList() = NullList()
  404.     DialogModel.lstSelFields.StringItemList() = NullList()
  405.     bEnableBinaryOptionGroup = False
  406. End Sub
  407.  
  408.  
  409. Sub DeleteFirstTableListBoxEntry()
  410.     DeleteFirstListBoxEntry("lstTables", sSelectDBTable)
  411. End Sub
  412.  
  413. Sub DeleteFirstListboxEntry(ListBoxName as String, DelEntryName as String)
  414. Dim oListbox as Object
  415. Dim sFirstItem as String
  416. dim iSelPos as Integer
  417.     oListBox = DlgFormDB.getControl(ListBoxName)
  418.     sFirstItem = oListBox.getItem(0)
  419.     If sFirstItem = DelEntryName Then
  420.         iSelPos = oListBox.getSelectedItemPos()
  421.         oListBox.removeItems(0, 1)
  422.         If iSelPos > 0 Then
  423.             oListBox.selectItemPos(iSelPos-1, True)
  424.         End If
  425.     End If
  426. End Sub
  427. </script:module>